﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cherry.Agile.RpcService.Client
{

    /// <summary>
    /// 客户端业务请求抽像基类
    /// </summary>
    public abstract class ClientRequest 
    {
        /// <summary>
        /// 业务代码
        /// </summary>
        /// <value></value>
        public virtual short BusinessCode { get; set; }

        /// <summary>
        /// 功能代码
        /// </summary>
        /// <value></value>
        public short FunctionCode { get; set; }

        /// <summary>
        /// 响应码
        /// </summary>
        /// <value></value>
        public ResponseCode ResponseCode { get; set; }
       
        /// <summary>
        /// 提交请求前将调用此过程，用于写入业务请求单元的数据
        /// </summary>
        /// <value></value>
        public Action<BufferWriter> OnWriteRequest { get; set; }

        /// <summary>
        /// 收到单元响应时将调用此过程，用于读取服务器返回的业务单元响应数据
        /// </summary>
        public Action<BufferReader> OnReadResponse { get; set; }

        /// <summary>
        /// 请求处理出现异常，或非正常响应时被调用
        /// </summary>
        public Action<Exception> OnError { get; set; }

        /// <summary>
        /// 处理当前请求单元流程控制器实例
        /// </summary>
        public IClientProcessor Processor { get; set; }


        /// <summary>
        /// 执行请求处理
        /// </summary>
        public virtual void Process()
        {
            Processor.Process(this);
        }
             

        /// <summary>
        /// 是否正常响应
        /// </summary>
        /// <value></value>
        public bool IsNormal
        {
            get { return ResponseCode == ResponseCode.Normal; }
        }
    }
}
